package Easy有效括号;

import org.testng.annotations.Test;

public class Solution {
    public boolean isValid(String s) {
        String T;
        int j=0;
        String[] Ts = s.split("");

        if(s.length()%2!=0)
            return false;

        for (int i = 0; i < s.length(); i++) {
            if("(".equals(Ts[i])) {
                T = s.substring(i,s.length());
                j = T.lastIndexOf(")");
                if ((j+1)%2!=0 || j==-1)
                    return false;
            }
            else if("[".equals(Ts[i])){
                T = s.substring(i,s.length());
                j = T.lastIndexOf("]");
                if ((j+1)%2!=0 || j==-1)
                    return false;
            }
            else if("{".equals(Ts[i])){
                T = s.substring(i,s.length());
                j = T.lastIndexOf("}");
                if ((j+1)%2!=0 || j==-1)
                    return false;
            }
            else if(")".equals(Ts[i])){
                T = s.substring(0,i+1);
                j = T.indexOf("(");
                if ((j+1)%2==0 || j==-1)
                    return false;
            }
            else if("]".equals(Ts[i])){
                T = s.substring(0,i+1);
                j = T.indexOf("[");
                if ((j+1)%2==0 || j==-1)
                    return false;
            }
            else if("}".equals(Ts[i])){
                T = s.substring(0,i+1);
                j = T.indexOf("{");
                if ((j+1)%2==0 || j==-1)
                    return false;
            }
            else
                continue;
        }
        return true;
    }

    @Test
    public void test(){
        String s="{[]}";
        System.out.println(isValid(s));
    }
}
